package team.se.tms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import team.se.tms.annotation.SpecialCheck;
import team.se.tms.dao.Dao;
import team.se.tms.dto.*;
import team.se.tms.exception.UpdateException;
import team.se.tms.po.AssignmentInfo;
import team.se.tms.dto.GroupDetail;
import team.se.tms.dto.Page;
import team.se.tms.dto.ParameterPage;
import team.se.tms.dto.ProjectDetail;
import team.se.tms.po.AssignmentInfo;
import team.se.tms.po.GroupInfo;
import team.se.tms.po.ProjectInfo;
import team.se.tms.po.StudentInfo;
import team.se.tms.service.ProjectInfoService;
import team.se.tms.util.FileUtil;
import team.se.tms.util.KeyUtil;

import javax.annotation.Resource;
import java.util.*;

/**
 * 项目信息业务层，具体实现项目信息业务接口层定义的方法
 *
 * @author winkyle
 * @date 2018/12/7
 */
@Slf4j
@Service("ProjectInfoServiceImpl")
public class ProjectInfoServiceImpl extends BaseServiceImpl implements ProjectInfoService {
    @Resource(name = "DaoImpl")
    private Dao dao;

    /**
     * 根据授课id, 获取分组详情
     *
     * @param teachCourseId 授课id
     * @param page 分页
     * @return 分组详情
     * @throws Exception 异常
     * @create on 2018/12/7 by winkyle
     */
    @Override
    public List<GroupDetail> listGroupDetails(@SpecialCheck("Pk")String teachCourseId, Page page) throws Exception {
        return dao.selectList("ProjectInfoMapper.selectGroupDetailsByTeachCourseId",getParameterPage(page,"teachCourseId",teachCourseId));
    }

    /**
     * 根据项目id部署项目
     *
     * @param projectId 项目id
     * @param projectAddress 项目地址
     * @return result
     * @throws Exception 异常
     * @create on 2018/12/24 by sunddly
     */
    @Override
    public Boolean modifyProjectAddress(String projectId, String projectAddress) throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("projectId",projectId);
        map.put("projectAddress",projectAddress);
        if((int) dao.update("ProjectInfoMapper.updateProjectAddressByProjectId",map)>0){
            //System.out.println("李四吗");
            return true;
        }
        //System.out.println("张三");
        return false;
    }
    /**
     * 根据用户id和授课id查看我的项目信息
     *
     * @param teachCourseId 授课id
     * @param userId 用户id
     * @return List<ProjectInfo>
     * @throws Exception 异常
     * @create on 2019/1/3 by sunddly
     */
    @Override
    public List<ProjectInfo> listProjectInfo(String teachCourseId, String userId) throws Exception {
        Map map =new HashMap<>();
        map.put("userId",userId);
        map.put("teachCourseId",teachCourseId);
       List<ProjectInfo> projectInfoList = dao.selectList("ProjectInfoMapper.selectMyProjectInfoByTeachCourseIdAndUserId",map);
        return projectInfoList;
    }
    /**
     * 查看我的项目详情
     *
     * @param projectId 项目Id
     * @return List<ProjectBasicDetail> 项目详情列表
     * @throws Exception 异常
     * @create on 2018/12/30 by sunddly
     */
    @Override
    public ProjectBasicDetail getProjectBasicDetail(String projectId) throws Exception {
        System.out.println("$$$$$$$$$$$$$"+projectId);
        //查询该授课下学生的项目信息
        ProjectBasicDetail projectBasicDetail = new ProjectBasicDetail();
        //添加项目信息
        ProjectInfo projectInfo = dao.selectOne("ProjectInfoMapper.selectMyProjectInfoByProjectId",projectId);
        System.out.println("$$$$$$$$$$$$$"+projectInfo);
        projectBasicDetail.setProjectInfo(projectInfo);
        //添加解析后的作业信息
        ParameterPage parameterPage =new ParameterPage();
        parameterPage.put("projectId",projectId);
        parameterPage.setPage(null);
        List<AssignmentInfo> assignmentInfoList =dao.selectList("AssignmentInfoMapper.selectAssignmentInfosByProjectId",parameterPage);
        System.out.println("assignmentInfoList%%%%%%%%%%%%%%%"+assignmentInfoList);
        List<AssignmentFile> assignmentFileList = FileUtil.analysisAssignmentFiles(assignmentInfoList);
        System.out.println("assignmentFileList%%%%%%%%%%%%%%%"+assignmentFileList);
        projectBasicDetail.setAssignmentFiles(assignmentFileList);
        //添加阶段链
        ProjectStageDetail projectStageDetail = dao.selectOne("Project_StageInfoMapper.selectProjectStageDetailByProjectId",projectId);
        projectBasicDetail.setProjectStageDetail(projectStageDetail);
        //添加小组信息
        List<StudentInfo> studentInfos =  dao.selectList("StudentInfoMapper.selectStudentInfosByProjectId",projectId);
        projectBasicDetail.setStudentInfos(studentInfos);
        return projectBasicDetail;
    }

    /**
     * 查询项目详情
     * @param page
     * @return
     * @throws Exception
     * @create on 2018/12/27 by qingx
     */
    @Override
    public List<ProjectDetail> listAllProjectDetails(Page page) throws Exception {
        List<ProjectDetail> projectDetails = dao.selectList("ProjectInfoMapper.selectAllProjectDetails",page);
        for (ProjectDetail projectDetail : projectDetails){
            String teammates = new String();
            if(!projectDetail.getStudentInfos().isEmpty()) {
                for (int i = 0; i < projectDetail.getStudentInfos().size() -1; i++) {
                    teammates += (projectDetail.getStudentInfos().get(i).getStuName() + "、");
                }
                teammates += projectDetail.getStudentInfos().get(projectDetail.getStudentInfos().size() -1).getStuName();
                projectDetail.setTeammates(teammates);
            }
        }
        return projectDetails;
    }

    /**
     * 添加项目信息
     *
     * @param projectInfo
     * @param stuIds
     * @return
     * @throws Exception
     * @create on 2018/12/28 by qingx
     * @update on 2018/12/31 by argentum
     */
    @Override
    public boolean savaProjectInfo(ProjectInfo projectInfo, String[] stuIds) throws Exception {
        projectInfo.setProjectId(KeyUtil.generatePrimaryKey());
        projectInfo.setProjectCreateTime(new Date());
        projectInfo.setProjectUpdateTime(new Date());
        int projectResult = dao.insert("ProjectInfoMapper.insertProjectInfo",projectInfo);
        if (projectResult != 0){
            ArrayList<GroupInfo> groupInfos = new ArrayList<>();
            for (int i = 0;i<stuIds.length;i++) {
                GroupInfo groupInfo = new GroupInfo();
                groupInfo.setProjectId(projectInfo.getProjectId());
                groupInfo.setStuId(stuIds[i]);
                if (i == 0){
                    groupInfo.setGroupIdentity(0);
                }else{
                    groupInfo.setGroupIdentity(1);
                }
                groupInfos.add(groupInfo);
            }
            int groupResult = dao.insert("GroupInfoMapper.insertGroupInfo",groupInfos);
            if (groupResult != 0){
                int result = dao.insert("Project_StageInfoMapper.insertProjectStageInfo", projectInfo);
                if (result != 0){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 修改项目信息
     * @param projectInfo
     * @param stuIds
     * @return
     * @throws Exception
     * @create on 2018/12/29 by qingx
     */
    @Override
    public boolean modifyProject(ProjectInfo projectInfo, String[] stuIds) throws Exception {
        projectInfo.setProjectUpdateTime(new Date());
        ParameterPage parameterPage =new ParameterPage();
        parameterPage.put("projectId",projectInfo.getProjectId());
        List<AssignmentInfo> assignmentInfos = dao.selectList("AssignmentInfoMapper.selectAssignmentInfosByProjectId",parameterPage);
        System.out.println("======================"+assignmentInfos);
        if (!assignmentInfos.isEmpty()){
            throw new UpdateException("该项目已提交文档，不允许修改");
        }
        int projectResult = dao.update("ProjectInfoMapper.updateProjectInfoByProjectId",projectInfo);
        if (projectResult == 0){
            return false;
        }
        int result = dao.delete("GroupInfoMapper.deleteGroupInfoByProjectId",projectInfo.getProjectId());
        if (result == 0){
            return false;
        }
        ArrayList<GroupInfo> groupInfos = new ArrayList<>();
        for (int i = 0;i<stuIds.length;i++) {
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setProjectId(projectInfo.getProjectId());
            groupInfo.setStuId(stuIds[i]);
            if (i == 0){
                groupInfo.setGroupIdentity(0);
            }else{
                groupInfo.setGroupIdentity(1);
            }
            groupInfos.add(groupInfo);
        }
        int groupResult = dao.insert("GroupInfoMapper.insertGroupInfo",groupInfos);
        if (groupResult == 0){
            return false;
        }
        int updateResult =  dao.update("Project_StageInfoMapper.updateProjectStageInfo", projectInfo);
        if (updateResult == 0){
            return false;
        }
        return true;
    }

//    /**
//     * 查询我的项目详情
//     *
//     * @param teachCourseId 授课Id
//     * @param studId 学生Id
//     * @param stageId 阶段Id
//     * @return List<ProjectDetail> 项目详情列表
//     * @throws Exception 异常
//     * @create on 2018/12/28 by sunddly
//     */
//    @Override
//    public List<ProjectDetail> listProjectDetails(String teachCourseId, String studId, String stageId) throws Exception {
//        return null;
//    }
}
